package com.onpositive.datamap.values.impl;

import java.util.ArrayList;

import com.onpositive.datamap.core.ITextSection;

public class TextSectionModel implements ITextSection {

	protected String title;

	protected CharSequence text;

	public CharSequence getText() {
		return text;
	}

	protected ArrayList<TextSectionModel> sections = new ArrayList<TextSectionModel>();

	protected TextSectionModel() {
	}
	
	public void accept(ITokenAcceptor acceptor){
		LinkParser.parseHyperlink(text.toString(), acceptor, "text");
	}

	public static final String SECTION_SEPARATOR = "<<< section separator >>>";

	public TextSectionModel(String content, final int id) {
		content = content.replaceAll("<!--.*-->", "");
		content = content.replaceAll("<ref.*ref>", "");
		content = content.replaceAll("<ref.*/>", "");
		StringBuilder bm = new StringBuilder();
		char pc = 0;
		boolean in = false;
		for (int a = 0; a < content.length(); a++) {
			char c = content.charAt(a);
			if (c == '{' && pc == '{') {
				in = true;
				bm.deleteCharAt(bm.length() - 1);
				bm.deleteCharAt(bm.length() - 1);
			}
			if (!in) {
				bm.append(c);
			} else {
				if (c == '}' && pc == '}') {
					in = false;
				}
			}
			pc = c;
		}
		content = bm.toString();
		String[] split = content.split(SECTION_SEPARATOR);
		text = new StringBuilder();
		

		for (String s : split) {
			String[] split2 = s.split("\n");
			if (split2.length >= 2) {
				String string = split2[0];
				String string1 = string.startsWith("name=")? string.substring("name=".length()):"";
				String name ="";
				String parent="";
				int ind=0;
				int k = string1.indexOf("parent=");
				if (k!=-1){
				name = string.substring(0, k).trim();
				parent = string.substring(k + "parent=".length()).trim();
				ind=1;
				}
				else{
					string = string.startsWith("name=")? string.substring("name=".length()):"";
					name=string;
					String str=split2[1];
					k = str.indexOf("parent=");
					parent = str.substring(k + "parent=".length()).trim();
					ind=2;
				}
				TextSectionModel ml = new TextSectionModel("", id);
				StringBuilder bld = new StringBuilder();
				// SpannableStringBuilder bld = new SpannableStringBuilder();
				for (int a = ind; a < split2.length; a++) {
					
					String trim = split2[a].trim();
					int length = trim.length();
					if (length > 0) {
						bld.append(trim);
						bld.append("\n");
					}
				}
				ml.text = bld;
				ml.title = name;
				if (parent.equals("null") || parent.equals("Intro") || parent.length()==0) {
					if (name.equals("Intro")) {
						this.text = bld;
					} else {
						sections.add(ml);
					}
				} else {
					if (name.equals("Intro")) {
						this.text = bld;
					} else {
						this.add(ml, parent);
					}
				}
			}
		}
	}

	private boolean add(TextSectionModel ml, String parent) {
		if (parent.equals(this.title)) {
			sections.add(ml);
			return true;
		}
		for (TextSectionModel m : sections) {
			if (m.add(ml, parent)) {
				return true;
			}
		}
		return false;
	}

	public ITextSection[] getChildren() {
		return sections.toArray(new ITextSection[sections.size()]);
	}

	public String getTitle() {
		return title;
	}

	public TextSectionModel find(String stringExtra) {
		if (stringExtra.equals(this.title)) {
			return this;
		}
		for (TextSectionModel m : sections) {
			TextSectionModel find = m.find(stringExtra);
			if (find != null) {
				return find;
			}
		}
		return null;
	}

	@Override
	public String name() {
		return title;
	}

	static LinkParser ps = new LinkParser();

	/**
	 * todo improve processing
	 */
	@Override	
	public void getParsedContent(ITokenAcceptor acceptor) {
		String[] split2 = text.toString().split("\n");
		for (int a = 1; a < split2.length; a++) {
			String trim = split2[a].trim();
			int length = trim.length();
			if (length > 0) {
				boolean stBold = trim.charAt(0) == ';';
				if (stBold) {
					for (int b = 1; b < trim.length(); b++) {
						char c = trim.charAt(b);
						if (c == ':') {
							acceptor.acceptPlainText(trim.substring(1, b),
									ITokenAcceptor.BOLD);
							trim = trim.substring(b + 1);
							break;
						}
					}
				}
			}
			LinkParser.parseHyperlink(trim, acceptor,null);
			acceptor.acceptValueSeparator();
		}
	}

}
